package com.fortune.app.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 数据库帮助类，用于管理本地SQLite数据库
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "fortune.db";
    private static final int DATABASE_VERSION = 1;
    
    private static DatabaseHelper instance;
    
    // 单例模式获取实例
    public static synchronized DatabaseHelper getInstance(Context context) {
        if (instance == null) {
            instance = new DatabaseHelper(context.getApplicationContext());
        }
        return instance;
    }
    
    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建用户历史记录表
        db.execSQL("CREATE TABLE history (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "type TEXT NOT NULL," +  // 占卜类型：周易、塔罗牌、星座、解梦等
                "query TEXT NOT NULL," +  // 查询内容或问题
                "result TEXT NOT NULL," +  // 结果JSON字符串
                "timestamp INTEGER NOT NULL" +  // 时间戳
                ")");
        
        // 创建梦境关键词表
        db.execSQL("CREATE TABLE dream_keywords (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "keyword TEXT NOT NULL," +
                "meaning TEXT NOT NULL," +
                "category TEXT NOT NULL" +
                ")");
        
        // 创建收藏表
        db.execSQL("CREATE TABLE favorites (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "history_id INTEGER NOT NULL," +
                "timestamp INTEGER NOT NULL," +
                "FOREIGN KEY(history_id) REFERENCES history(_id)" +
                ")");
        
        // 初始化梦境关键词数据
        initDreamKeywords(db);
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 数据库升级逻辑
        if (oldVersion < 2) {
            // 未来版本升级时添加
        }
    }
    
    /**
     * 初始化梦境关键词数据
     */
    private void initDreamKeywords(SQLiteDatabase db) {
        // 人物类
        insertDreamKeyword(db, "父亲", "代表权威、指导、保护或控制", "人物");
        insertDreamKeyword(db, "母亲", "代表养育、关爱、保护或依赖", "人物");
        insertDreamKeyword(db, "孩子", "代表纯真、新生、潜力或责任", "人物");
        insertDreamKeyword(db, "老人", "代表智慧、经验、传统或过去", "人物");
        insertDreamKeyword(db, "陌生人", "代表未知的自我方面或新的可能性", "人物");
        
        // 动物类
        insertDreamKeyword(db, "蛇", "代表转变、智慧、诱惑或恐惧", "动物");
        insertDreamKeyword(db, "鸟", "代表自由、灵性、思想或愿望", "动物");
        insertDreamKeyword(db, "狗", "代表忠诚、友谊、保护或服务", "动物");
        insertDreamKeyword(db, "猫", "代表独立、神秘、女性特质或直觉", "动物");
        insertDreamKeyword(db, "马", "代表力量、自由、激情或欲望", "动物");
        
        // 物品类
        insertDreamKeyword(db, "房子", "代表自我、安全、庇护或心理状态", "物品");
        insertDreamKeyword(db, "车", "代表人生方向、控制或进展", "物品");
        insertDreamKeyword(db, "钱", "代表价值、自尊、力量或安全感", "物品");
        insertDreamKeyword(db, "书", "代表知识、学习、记忆或智慧", "物品");
        insertDreamKeyword(db, "门", "代表机会、过渡、选择或障碍", "物品");
        
        // 自然元素
        insertDreamKeyword(db, "水", "代表情感、潜意识、净化或变化", "自然");
        insertDreamKeyword(db, "火", "代表激情、转变、破坏或净化", "自然");
        insertDreamKeyword(db, "风", "代表变化、自由、思想或沟通", "自然");
        insertDreamKeyword(db, "山", "代表挑战、成就、稳定或障碍", "自然");
        insertDreamKeyword(db, "树", "代表成长、生命、家族或个人发展", "自然");
        
        // 情境类
        insertDreamKeyword(db, "飞行", "代表自由、超越限制、逃避或灵性提升", "情境");
        insertDreamKeyword(db, "坠落", "代表失控、恐惧、失败或释放", "情境");
        insertDreamKeyword(db, "追逐", "代表逃避、恐惧、压力或未解决的问题", "情境");
        insertDreamKeyword(db, "考试", "代表评估、压力、准备或自我怀疑", "情境");
        insertDreamKeyword(db, "婚礼", "代表承诺、结合、转变或新阶段", "情境");
    }
    
    /**
     * 插入梦境关键词
     */
    private void insertDreamKeyword(SQLiteDatabase db, String keyword, String meaning, String category) {
        db.execSQL("INSERT INTO dream_keywords (keyword, meaning, category) VALUES (?, ?, ?)",
                new Object[]{keyword, meaning, category});
    }
}
